<template>
    <div class="mod-prod">
        <avue-crud ref="crud"
                   :data="dataList"
                   :option="tableOption"
                   :page="page"
                   :permission="permission"
                   :table-loading="dataListLoading"
                   @search-change="searchChange"
                   @selection-change="selectionChange"
                   @on-load="getDataList"
                   @row-save="sourceRowSave"
                   @row-del="rowDel"
				   @refresh-change="getDataList(page)"
				   >


            <template slot="status"
                      slot-scope="scope">
                <el-tag v-if="scope.row.status === 1"
                        size="small">上架
                </el-tag>
                <el-tag v-else
                        size="small">未上架
                </el-tag>
            </template>

            <template slot="menu"
                      slot-scope="scope">
                
                <el-button
                    icon="el-icon-delete"
                    size="small"
                    type="danger"
                    @click="deleteHandle(scope.row.id)">删除
                </el-button>
            </template>
        </avue-crud>
    </div>
</template>

<script>
import {tableOption} from '@/crud/prod/prodList'

export default {
	data () {
		return {
			dataForm: {
				prodName: ''
			},
			permission: {
				delBtn: this.isAuth('prod:prod:delete')
			},
			dataList: [],
			page: {
				total: 0, // 总页数
				currentPage: 1, // 当前页数
				pageSize: 1000, // 每页显示多少条
                pageSizes: [1000]
			},
			dataListSelections: [],
			dataListLoading: false,
			tableOption: tableOption,
			resourcesUrl: process.env.VUE_APP_RESOURCES_URL
		}
	},
	methods: {
		// 获取数据列表
		getDataList (page, params, done) {
			this.dataListLoading = true
			this.$http({
				url: this.$http.adornUrl('/crm/base/prod/page'),
				method: 'get',
				params: this.$http.adornParams(
					Object.assign(
						{
							current: page == null ? this.page.currentPage : page.currentPage,
							size: page == null ? this.page.pageSize : page.pageSize
						},
						params
					)
				)
			}).then(({data}) => {
				this.dataList = data.records
				this.page.total = data.total
				this.dataListLoading = false
				if (done) {
					done()
				}
			})
		},
		// 新增 / 修改
		addOrUpdateHandle (id) {
			this.$router.push({
				path: '/prodInfo',
				query: {prodId: id}
			})
		},
		// 删除和批量删除
		deleteHandle (id) {
			this.$confirm(`确定进行删除操作?`, '友情提醒', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			})
				.then(() => {
					this.$http({
						url: this.$http.adornUrl(`/prod/prod`),
						method: 'delete',
						data: this.$http.adornData({id: id}, false)
					}).then(({data}) => {
						this.$message({
							message: '操作成功',
							type: 'success',
							duration: 1500,
							onClose: () => {
								this.getDataList(this.page)
							}
						})
					})
				})
				.catch(() => {
				})
		},
        rowDel: function (row, index) {
            this.$confirm('确定进行删除操作?', '友情提醒', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http({
                    url: this.$http.adornUrl('/crm/base/prod/' + row.id),
                    method: 'delete',
                    data: this.$http.adornData({})
                }).then(({data}) => {
                    this.$message({
                        message: '操作成功',
                        type: 'success',
                        duration: 1500,
                        onClose: () => {
                            this.getDataList()
                        }
                    })
                })
            }).catch(() => {
            })
        },
        sourceRowSave (form, done, loading) {
            console.log('🚀 ~ form', form)
            this.$http({
                url: this.$http.adornUrl('/crm/base/prod/saveProd'),
                method: 'post',
                data: this.$http.adornData({
                    prodId: wfy.uuid(20),
                    prodCode: form.prodCode,
                    prodName: form.prodName,
                    prodSpec: form.prodSpec,
                    prodLenwidth: form.prodLenwidth,
                })
            }).then(({data}) => {
                this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                        if (done) {
                            done()
                        }
                        this.getDataList(this.page)
                    }
                })
            })
        },
		// 条件查询
		searchChange (params, done) {
			this.getDataList(this.page, params, done)
		},
		// 多选变化
		selectionChange (val) {
			this.dataListSelections = val
		},
		// 获取选中的商品Id列表
		getSeleProdIds () {
			return this.dataListSelections.map(item => {
				return item.prodId
			})
		}
	}
}
</script>
